package com.durian.base.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.durian.BaseApp;
import com.gyf.immersionbar.ImmersionBar;
import java.lang.reflect.Method;

/**
 * 说明：UIUtils
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-17 14:10
 * <p/>
 * 版本：version 1.0
 */
public class UIUtils {
    /**
     * 说明：禁止实例化
     */
    private UIUtils(){}

    public static int getDimensionPixelSize(@DimenRes int dimen){
        return BaseApp.getApp().getResources().getDimensionPixelOffset(dimen);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(double dpValue) {
        final double scale = BaseApp.getApp().getResources().getDisplayMetrics().density;
        dpValue = dpValue * scale;
        return (int) Math.ceil(dpValue);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(double pxValue) {
        final double scale = BaseApp.getApp().getResources().getDisplayMetrics().density;
        pxValue = NumberUtils.divide(pxValue,scale);
        return (int) Math.ceil(pxValue);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 sp
     */
    public static int px2sp(float pxValue) {
        float fontScale = BaseApp.getApp().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 sp 的单位 转成为 px
     */
    public static int sp2px(float spValue) {
        float fontScale = BaseApp.getApp().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 获取资源
     * @return
     */
    private static Resources getResource() {
        return BaseApp.getApp().getResources();
    }

    /**
     * 返回String数组
     * @param id  资源id
     * @return
     */
    public static String[] getStringArray(int id) {
        return getResource().getStringArray(id);
    }

    /**
     * 说明：获取字符串资源
     * @param id
     * @return
     */
    public static String getString(int id) {
        return getResource().getString(id);
    }
    /**
     * 说明：获取颜色资源
     * @param id
     * @return
     */
    public static int getColor(int id) {
        return getResource().getColor(id);
    }
    /**
     * 获取ColorStateList
     * @param color
     * @return
     */
    public static ColorStateList getColorStateList(@ColorRes int color){
        return ColorStateList.valueOf(getColor(color));
    }
    /**
     * 说明：获取屏幕的宽度
     * @return
     */
    public static int screenWidth(){
        WindowManager wm = (WindowManager) BaseApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取view的宽度
     * @param view
     * @return
     */
    public static int getMeasuredWidth(View view){
        int width = 0;
        if (view != null){
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            width = view.getMeasuredWidth();
        }
        return width;
    }

    /**
     * 获取view的高度
     * @param view
     * @return
     */
    public static int getMeasuredHeight(View view){
        int height = 0;
        if (view != null){
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            height = view.getMeasuredHeight();
        }
        return height;
    }

    /**
     * 说明：获取屏幕的高度
     * @return
     */
    public static int screenHeight(){
        WindowManager wm = (WindowManager) BaseApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }
    /**
     * 说明：获取屏幕的密度
     * @return
     */
    public static float density(){
        WindowManager wm = (WindowManager) BaseApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.density;
    }

    /**
     * 说明：获取状态栏的高度
     * @return
     */
    public static int getStatusBarHeight(Activity activity){
        int height = 0;
        try {
            height = ImmersionBar.getStatusBarHeight(activity);
        }catch (Exception e){
            LogUtils.e(e);
        }
        return height;
    }

    /**
     * 说明：获取布局文件
     * @param id
     * @param group
     * @param flag
     * @return
     */
    public static View inflate(int id, ViewGroup group, boolean flag){
        return LayoutInflater.from(BaseApp.getApp()).inflate(id, group, flag);
    }
    /**
     * 说明：获取布局文件
     * @param id
     * @return
     */
    public static View inflate(int id){
        return LayoutInflater.from(BaseApp.getApp()).inflate(id, null, false);
    }

    public static void setWidthAndHeight(View view,int w,int h){
        if (view != null){
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.width = w;
            params.height = h;
            view.setLayoutParams(params);
        }
    }

    /**
     * 转换颜色
     * @param src
     * @return
     */
    public static int parseColor(String src){
        int color = Color.WHITE;
        try {
            color = Color.parseColor(src);
        }catch (Exception e){
            e.printStackTrace();
        }
        return color;
    }

    /**
     * 检查是否存在虚拟按键栏
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean hasNavBar(Context context) {
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判断虚拟按键栏是否重写
     *
     * @return
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
            }
        }
        return sNavBarOverride;
    }
}
